package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;

import com.itheima.pojo.vo.MemberVO;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("orderList")
public class OrderController {



    @Reference
    OrderService orderService;

    @Reference
    MemberService memberService;



    @RequestMapping("findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult= orderService.findByPage(queryPageBean);

        return pageResult;

    }

    @RequestMapping("isOrder")
    public Result isOrder(int id){
        try {
            orderService.updateOrderStatusById(id);
            return new Result(true, MessageConstant.UPDATE_ORDER_STATUS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.UPDATE_ORDER_STATUS_FAIL);
        }
    }

    @RequestMapping("notOrder")
    public Result notOrder(int id){
        try {
            orderService.updateNotOrderStatusById(id);
            return new Result(true, MessageConstant.UPDATE_ORDER_STATUS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.UPDATE_ORDER_STATUS_FAIL);
        }
    }

    @RequestMapping("addMember")
    public Result addMember(@RequestBody Map map,int[] setMealids) throws Exception {


        String birthday = (String) map.get("birthday");
        String phoneNumber = (String) map.get("phoneNumber");
        String idCard = (String) map.get("idCard");
        String sex = (String) map.get("sex");
        String name = (String) map.get("name");
        String maritalStatus = (String) map.get("maritalStatus");
        String age = (String) map.get("age");
        String orderDate = (String) map.get("orderDate");

        MemberVO member = new MemberVO();
        member.setBirthday(DateUtils.parseString2Date(birthday));
        member.setPhoneNumber(phoneNumber);
        member.setIdCard(idCard);
        member.setSex(sex);
        member.setName(name);
        member.setMaritalStatus(maritalStatus);
        member.setAge(Integer.parseInt(age));
        member.setRegTime(new Date());
        memberService.addMemberAndOrder(member,setMealids,orderDate);


        return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
    }


    @RequestMapping("outExcel")
    public Result outExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {

        try {
            //获取数据
            List<Map<String, Object>> list = orderService.getOrderReportData();

            //创建excel对象
            String filePath = request.getSession().getServletContext().getRealPath("template") + File.separator + "order_template.xlsx";
            XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File(filePath)));
            //读取第一个工作表
            XSSFSheet sheet = excel.getSheetAt(0);
            //行
            int x = 1;

            for (Map<String, Object> map : list) {
                Integer peopleNumAble = (Integer) map.get("number");
                Integer peopleNumHas = (Integer) map.get("reservations");
                String orderDate = DateUtils.parseDate2String((Date)map.get("orderDate"));


                //获取第x行
                XSSFRow row = sheet.createRow(x);
                row.createCell(0).setCellValue(orderDate);//日期
                row.createCell(1).setCellValue(peopleNumAble);//可预约人数
                row.createCell(2).setCellValue(peopleNumHas);//已预约人数
                x++;
            }


            ServletOutputStream os = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition","attachment;filename=OrderReport.xlsx");
            excel.write(os);
            os.flush();
            os.close();
            excel.close();

            return new Result(true,MessageConstant.GET_ORDER_DATA_SUCCESS);
        } catch (IOException e) {
            return new Result(true,MessageConstant.GET_ORDER_DATA_FAIL);
        }
    }


}
